from src.settings import CHROME_DRIVER_PATH,CHATGPT_URL
from src.algo.selenium.ChatgptSelenium import ChatgptSelenium
from src.algo.selenium.PeiYinShenQi import PeiYinShenQiSelenium
import time
import re
import sys
import os
import pandas as pd



def get_text_from_chatgpt(originText,chatgpt_selinium,):
    '''
    根据文案调用chatgpt生成新的文案
    :param item:
    :param chatgpt_selinium:
    :param wenan_dir:
    :return:
    '''

    if len(originText) <= 30:
        return ""


    promptText = '''【{text}】

假如你是个文案编辑师。从下面几个方面入手，重写【】内的文章：
1.语气更加幽默风趣一些
2.适当增加比喻、对比、反问等文学手法来丰富文章风格。
3.添加一些个人理解感受，要积极向上
请帮我按照以上要求，生成2个完全不同的文章
    '''

    response_text = chatgpt_selinium.get_prompt_result(promptText.format(text=originText))
    time.sleep(4)
    return response_text



def main(excelPath):
    '''

    :param excelPath: 原始文案地址
    :param finalSavePath: 最终文案输出地址
    :param chatgptUrl: 在线gpt网站
    :return:
    '''
    df = pd.read_excel(excelPath)
    df.fillna("",inplace=True)


    if "文案重写" not in df.columns:
        df = df.assign(文案重写="")
        df.to_excel(excelPath,index=False)

    peyinshenqi_selenium = PeiYinShenQiSelenium(driver_path=CHROME_DRIVER_PATH)
    for index,(_,row) in enumerate(df.iterrows()):
        row = dict(row)
        if row["文案"] == "":
            df.at[index,"文案"] = peyinshenqi_selenium.get_tts_wenan(row["视频链接"])
            df.to_excel(excelPath,index=False)
    peyinshenqi_selenium.browser.stop_client()
    df = pd.read_excel(excelPath)
    df.fillna("", inplace=True)
    chatgpt_selenium = ChatgptSelenium(chatgpt_url=CHATGPT_URL, driver_path=CHROME_DRIVER_PATH)
    for index,(_,row) in enumerate(df.iterrows()):
        row = dict(row)
        if row["文案重写"] == "":
            df.at[index,"文案重写"] = get_text_from_chatgpt(row["文案"],chatgpt_selenium)
            df.to_excel(excelPath,index=False)





if __name__ == '__main__':
    rootPath = r'F:\fanfan'
    excelPath = os.path.join(rootPath,"1010.xlsx")
    main(excelPath)